﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using System.Data;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.Finiance
{
    public partial class CustomerBillView : BasePage
    {
        public string SortString
        {
            get {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "dDate";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["CustomerCode"] != null)
                {
                    BindGrid();
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            BillBll bll = new BillBll();
            int RecordCount = 0;
            DataTable dt = bll.GetBillPerformance(" and cCusCode = '" + Request.QueryString["CustomerCode"] + "'", SortString, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out RecordCount);
            gridBills.DataSource = dt;
            gridBills.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void btnRefershBillInfo_Click(object sender, EventArgs e)
        {
            if (txtcBVCode.Value == "") return;

            //BillBll bll = new BillBll();
            //CtrtBillEntity entity = bll.Get<CtrtBillEntity>(" and cBVCode = '"+txtcBVCode.Value+"'");
            //if (entity != null)
            //{
            //    CtrtCustomerEntity cusEnt = bll.Get<CtrtCustomerEntity>(" and CustomerCode= '" + txtcBVCode .Value+ "'");
            //    if (cusEnt != null)
            //        lblCustomerName.Text = cusEnt.CustomerName;
            //    else lblCustomerName.Text = "";

            //    lblcCusCode.Text = txtcBVCode.Value;
            //    lblclnvCode.Text = entity.cInvCode;
            //    lblcMemo.Text = entity.cMemo;
            //    lbldDate.Text = entity.dDate.ToShortDateString();
            //    lbliNatMoney.Text = entity.iNatMoney.ToString();
            //    lbliNatSum.Text = entity.iNatSum.ToString();
            //    lbliNatTax.Text = entity.iNatTax.ToString();
            //    lbliQuantity.Text = entity.iQuantity.ToString();
            //    lbliTaxUnitPrice.Text = entity.iTaxUnitPrice.ToString();
            //}
            BillBll bll = new BillBll();
            int RecordCount = 0;
            DataTable dt = bll.GetBills(" and cBVCode = '" + txtcBVCode.Value + "'", "cBVCode", AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex, out RecordCount);
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }

        protected void gridBills_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy =  e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            BindGrid();
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = "";

            if (Request.QueryString["CustomerCode"].Trim() != "")
            {
                Whereclause = " and cCusCode = '" + Request.QueryString["CustomerCode"] + "'";
            }

            BillBll bll = new BillBll();

            DataTable dt = bll.ExportCustomerBills(Whereclause, "dDate");

            Export.Export2Excel(dt,"客户发票应收账款", Page.Response);

        }
 
        protected void gridBills_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView dv = e.Item.DataItem as DataRowView;
                if (dv != null)
                {
                    e.Item.Attributes.Add("alerttype ", dv["alerttype"].ToString());
                } 
            }
        } 

    }
}
